<template>
  <div>
    <el-form :model="searchForm" inline>
      <el-form-item label="分类名称">
        <el-input placeholder="外链名称" v-model="searchForm.categoryName" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select placeholder="选择状态" clearable v-model="searchForm.status">
          <el-option :value="1" label="可用"/>
          <el-option :value="2" label="禁用"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button @click="openDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table border :data="dataList">
      <el-table-column prop="categoryId" label="编号" align="center"/>
      <el-table-column prop="categoryName" label="分类名称" align="center"/>
      <el-table-column prop="profile" label="描述" align="center"/>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">可用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row.categoryId)">修改</el-button>
          <el-button size="small" type="warning" @click="deleteInfo(scope.row.categoryId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-if="total>0" :current-page="searchForm.page" :page-size="searchForm.limit" :total="total" :page-sizes="[5,10,20,30,50,100]" layout="total,sizes,prev,pager,next,jumper" @current-change="currentChange" @size-change="sizeChange"></el-pagination>
    <CategoryUpdate ref="categoryUpdate" @onCloseDialog="getDataList"/>
  </div>
</template>

<script>
import CategoryUpdate from '@/views/admin/category/CategoryUpdate'
export default {
  name: 'CategoryList',
  components: { CategoryUpdate },
  data () {
    return {
      searchForm: {
        page: 1,
        limit: 5,
        status: '',
        categoryName: ''
      },
      dataList: [],
      total: 0
    }
  },
  methods: {
    getDataList () {
      this.$http.get('admin/category/page', this.searchForm).then(data => {
        this.dataList = data.data.records
        this.searchForm.page = data.data.current
        this.searchForm.limit = data.data.size
        this.total = data.data.total
      })
    },
    openDialog (categoryId) {
      this.$refs.categoryUpdate.init(categoryId)
    },
    deleteInfo (categoryId) {
      this.$confirm(`确定删除编号为[${categoryId}]的数据吗？`).then(() => {
        this.$http.delete(`admin/category/delete/${categoryId}`).then(() => {
          this.$alert('删除成功！', { type: 'success' }).then(() => {
            this.getDataList()
          })
        })
      })
    },
    currentChange (value) {
      this.searchForm.page = value
      this.getDataList()
    },
    sizeChange (value) {
      this.searchForm.limit = value
      this.getDataList()
    }
  },
  created () {
    this.$nextTick(() => {
      this.getDataList()
    })
  }
}
</script>

<style scoped>

</style>
